export default {
  props: ['value', 'item'],
  data() {
    return {}
  },
  computed: {},
  created() {

  },
  watch: {},
  mounted() {

  },
  methods: {
    beforeUpload(file) {
      let sizeUnitNum = 1
      // 文件大小判断
      switch (this.item.config.sizeUnit) {
        case 'KB':
          sizeUnitNum = 1024
          break
        case 'MB':
          sizeUnitNum = 1024 * 1024
          break
        case 'GB':
          sizeUnitNum = 1024 * 1024 * 1024
          break
      }
      let totalSize = this.item.config.fileSize * sizeUnitNum
      if (file.size > totalSize) {
        this.$message.error(`上传文件大小不能超过${this.item.config.fileSize}${this.item.config.sizeUnit}`)
        return false
      }
      let isAccept = true


      const fileIndex = file.name.lastIndexOf('.')
      const fileType = file.name.toLowerCase().substring(fileIndex)

      // 判断是否是数组
      if (this.item.accept&&Array.isArray(this.item.accept)) {
        for (let ac of this.item.accept){
          if (ac.indexOf("*") > -1) {
            isAccept = new RegExp(ac).test(fileType) || new RegExp(ac).test(file.type)
          } else {
            let rules = ac.split(',')
            for (let item of rules) {
              isAccept = false
              if (item === fileType) {
                isAccept = true
                break
              }
            }
          }
          if(isAccept){
            break
          }
        }
      }
      if (!isAccept) {
        this.$message.error(`应该选择${this.item.accept}类型的文件`)
        return false
      }
      return true
    },
    beforeUpload2(file,col,cIndex) {
      let sizeUnitNum = 1
      // 文件大小判断
      switch (col.columnForm.fileUnit) {
        case 'KB':
          sizeUnitNum = 1024
          break
        case 'MB':
          sizeUnitNum = 1024 * 1024
          break
        case 'GB':
          sizeUnitNum = 1024 * 1024 * 1024
          break
      }
      if(col.columnForm.fileSize){
        let totalSize = col.columnForm.fileSize * sizeUnitNum
        if (file.size > totalSize) {
          this.$message.error(`上传文件大小不能超过${col.columnForm.fileSize}${col.columnForm.fileUnit}`)
          return false
        }
      }
      
      if(col.columnForm.fileType){
        let isAccept = true
        const fileIndex = file.name.lastIndexOf('.')
        const fileType = file.name.toLowerCase().substring(fileIndex)
        // 判断是否是数组
        if (col.columnForm.fileType) {
            var ac = col.columnForm.fileType
          // for (let ac of col.columnForm.fileType){
            if (ac.indexOf("*") > -1) {
              isAccept = new RegExp(ac).test(fileType) || new RegExp(ac).test(file.type)
            } else {
              let rules = ac.split(',')
              for (let item of rules) {
                isAccept = false
                if (item === fileType) {
                  isAccept = true
                  break
                }
              }
            }
            // if(isAccept){
            //   break
            // }
          // }
        }
        if (!isAccept) {
          this.$message.error(`应该选择${col.columnForm.fileType}类型的文件`)
          return false
        }
      }
      
      return true
    }

  }
}
